<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/3/14 23:17
 * @version
 */
namespace Modules\Admin\Main\Controllers;

use App\Http\Controllers\Controller;
use App\Models\AppControl;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AppController extends Controller
{
    public function index()
    {
        return view('main::app_control', \Request::all());
    }

    public function all(Request $request)
    {
        $data = [];
        $data['total'] = AppControl::query()->count();
        $data['items'] = AppControl::query()->get();
        return success($data);
    }

    public function add(Request $request)
    {
        $data = $request->only(['title','app_name','channel','current_version','update_to_version','download_url',
        'is_update','force_update','open_weixin_login','update_words']);

        $validator = Validator::make($request->all(), [
            'title'           => 'required',
            'app_name'        => 'required',
            'channel'         => 'required',
            'current_version' => 'required'
        ]);
        if ($validator->fails()) {
            return fail(400, $validator->errors()->first());
        }

        AppControl::create($data);
        $this->recordAction('添加了新应用' . $data['title']);
        return success();

    }

    public function updateBool(Request $request)
    {
        $id = $request->input('id');
        $field = $request->input('field');
        $value = $request->input('value');
        $info = AppControl::find($id);
        $data = null;
        if(!$info){
            return fail(400,'数据不存在');
        }

        if($field=='is_update') $data['is_update'] = $value;
        if($field=='force_update') $data['force_update'] = $value;
        if($field=='open_weixin_login') $data['open_weixin_login'] = $value;

        if(!$data){
            return fail(400,'数据不存在');
        }

        $info->update($data);
        return success();
    }

    public function update(Request $request)
    {
        $id = $request->input('id');
        $data = $request->only(['title','app_name','channel','current_version','update_to_version','download_url',
            'is_update','force_update','open_weixin_login','update_words']);

        $validator = Validator::make($request->all(), [
            'title'           => 'required',
            'app_name'        => 'required',
            'channel'         => 'required',
            'current_version' => 'required'
        ]);
        if ($validator->fails()) {
            return fail(400, $validator->errors()->first());
        }
        $info = AppControl::find($id);
        if(!$info){
            return fail(400,'数据不存在');
        }
        $info->update($data);
        $this->recordAction('更新了应用<' . $data['title'] . '>',$data);
        return success();
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        $res = AppControl::find($id);
        $res->delete();
        $this->recordAction('删除了应用<' . $res['title'] . '>',$res->toArray());
        return success();
    }


}